<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
	use HasDateTimeFormatter;

    protected $fillable = [
        'user_id',
        'name',
        'cover',
        'content',
        'price',
        'points',
        'sale_type',
        'firm_profession_id',
        'pay_method',
        'platform'
    ];

    protected $attributes = [
        'status' => 1,
        'audit_status' => 1,
        'reject_reason' => '',
        'points'=> 0,
        'price' => 0,
        'sort' => 0
    ];

    public function identity()
    {
        return $this->belongsTo(UserIdentity::class,'user_id','user_id');
    }

    public function chapter()
    {
        return $this->hasMany(CourseChapter::class,'course_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class,'user_id');
    }

    public function category()
    {
        return $this->belongsTo(FirmProfession::class,'firm_profession_id');
    }

    public function auth()
    {
        return $this->belongsTo(UserAuth::class,'user_id','user_id');
    }

    public function order()
    {
        return $this->hasOne(CourseOrder::class,'course_id');
    }

    public function collect()
    {
        return $this->hasMany(UserCollectCourse::class,'course_id');
    }


    public function getContentAttribute($value)
    {
        return str_replace("<img ", "<img style='width:100%;height:100%;vertical-align:middle;' ",$value);
    }



    public function like()
    {
        return $this->hasMany(UserLikeCourse::class,'course_id');
    }

    public function comment()
    {
        return $this->hasMany(CourseComment::class,'course_id');
    }
}
